import $H from '../../utils/request.js';
import Chat from "@/common/free-lib/chat.js"
export default {
	state: {
		user: false,
		apply: {},
		chat: null,
		totalNoreadnum: 0,
		chatList: [],
		mailList: []
	},
	actions: {
		login({
			state,
			dispatch
		}, user) {
			console.log(user, "user")
			state.user = user
			// state.chat=new chat({url:"ws://127.0.0.1:7001/ws?token=" + state.user.token})
			console.log(state.user, "useruser")
			uni.setStorageSync("token", user.token);
			uni.setStorageSync("user", JSON.stringify(user));
			uni.setStorageSync("user_id", user.id)
			// dispatch('initlogin')
			state.chat = new Chat({
				url: "ws://127.0.0.1:7001/ws?token=" + state.user.token
			})

			dispatch('getApply')
			dispatch("getMailList")
			dispatch("getchatlist")
			dispatch("getupdatebadge")
		},
		logout({
			state
		}) {
			$H.post("user/logout", {}, {
				token: true
			}).then(res => {
				uni.showToast({
					title: res.msg
				})
				uni.removeStorage({
					key:"token"
				})
// uni.removeStorage("token")
				// uni.clearStorageSync();
				console.log(state.chat, "123")
				if (state.chat) {
					console.log(state.chat)

					state.chat.getName()
					console.log(1233);
					state.chat = null
				}

				uni.navigateTo({
					url: "/pages/login/login"
				})

			})
		},
		initlogin({
			state,
			dispatch
		}) {
			if (uni.getStorageSync("user")) {
				state.user = JSON.parse(uni.getStorageSync("user"))
				state.chat = new Chat({
					url: "ws://127.0.0.1:7001/ws?token=" + state.user.token
				})
				// let socket=uni.connectSocket({
				// 	url:"ws://127.0.0.1:7001/ws?token="+state.user.token,
				// 	complete: () => {

				// 	}
				// })
				// socket.onOpen(()=>{
				// 	console.log("socket已连接");
				// })
				// socket.onMessage((res)=>{
				// 	console.log(res,"接收消息");
				// })
				// socket.onClose(()=>{
				// 	console.log("socket已断开");						
				// })
				// socket.onError((res)=>{
				// 	console.log(res,"socket连接错误");
				// })
				dispatch('getApply')
				dispatch("getchatlist")
				dispatch("getMailList")
				dispatch("getupdatebadge")
			}
		},
		getApply({
			state,
			dispatch
		}) {
			$H.post("apply/list/1").then(res => {
				console.log(res, "res")
				state.apply = res
				dispatch('updateMailBadge')
			})
		},
		getchatlist({
			state
		}) {
			
			uni.$on('onUpdateChatList', (list) => {

				console.log(list,"list")
				state.chatList = list
				// let total=0
				// list.forEach(el=>{
				// 	total+=el.noreadnum
				// })
				// state.totalNoreadnum=total
			})
			state.chatList = state.chat.getChatlist()
		},
		getMailList({
			state
		}) {
			$H.post("friend/list",{token:true}).then(res => {
				console.log(res, "mail")
				if(res){
		state.mailList = res.rows.newList ? res.rows.newList : []
					
				}
			})
		},
		getupdatebadge({
			state
		}) {
			uni.$on("totalNoreadnum", num => {
				state.totalNoreadnum =Number(num)
				console.log("num",num);
				
			})
			state.chat.updateBadge()
		},
		updateMailBadge({
			state
		}) {
			let count = state.apply.count;
			console.log(count, "count")
			if (count > 0) {
				uni.setTabBarBadge({
					index: 1,
					text: count.toString()
				})
			} else {
				console.log("移除");
				uni.removeTabBarBadge({
					index: 1,
					success: () => {
						console.log("移除成功")
					},
					fail: (err) => {
						console.log(err, "err");
					}
				})
			}


		}

	}
}
